﻿//-----------------------------------------------------------------------
// <copyright file="Subscription(THandler).cs" company="iron9light">
//      Copyright (c) iron9light. All rights reserved.
// </copyright>
// <author>iron9light</author>
//-----------------------------------------------------------------------

namespace EventBroker
{
    using System;
    using System.Collections.Generic;

    /// <summary>
    /// Represents a topic subscription with specialized handler.
    /// </summary>
    /// <typeparam name="THandler">The type of the handler.</typeparam>
    public class Subscription<THandler>
        : Subscription
        where THandler : IHandler, new()
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="Subscription&lt;THandler&gt;"/> class.
        /// </summary>
        /// <param name="subscriber">The subscriber.</param>
        /// <param name="handlerMethodName">Name of the handler method.</param>
        public Subscription(object subscriber, string handlerMethodName)
            : base(subscriber, handlerMethodName)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="Subscription&lt;THandler&gt;"/> class.
        /// </summary>
        /// <param name="subscriber">The subscriber.</param>
        /// <param name="handlerMethodName">Name of the handler method.</param>
        /// <param name="parameterTypes">The parameter types.</param>
        public Subscription(object subscriber, string handlerMethodName, Type[] parameterTypes)
            : base(subscriber, handlerMethodName, parameterTypes)
        {
        }

        /// <summary>
        /// Called when event topic fire.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        /// <param name="exceptions">The exceptions.</param>
        protected override void OnEventTopicFire(object sender, EventArgs e, ICollection<Exception> exceptions)
        {
            object subscriber = this.Subscriber;
            if (subscriber == null)
            {
                return;
            }

            IHandler handler = new THandler();
            handler.SubscriptionHandler = this.CreateSubscriptionDelegate();
            handler.Handle(sender, e, exceptions);
        }
    }
}